package struts.action;

import javax.naming.InitialContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cart.CartItem;

import struts.actionform.AddToCartForm;

import ejb.bl.CartBeanRemote;
import ejb.bl.MusicStoreBLRemote;

public class AddToCartAction extends Action {

	// Define EJB Session Bean
	private CartBeanRemote CartBean;
	private MusicStoreBLRemote MusicStoreBLBean;

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession();
		InitialContext ic=null;
		if(session.getAttribute("sessionContext")==null){
			ic = new InitialContext();
			session.setAttribute("sessionContext",ic);
		}else{
			ic =(InitialContext)session.getAttribute("sessionContext");
		}
		
		CartBean = (CartBeanRemote)session.getAttribute("CartBean");
		String forward2Page = null;
		AddToCartForm objForm = (AddToCartForm) form;
		
		//CartBean = (CartBeanRemote) ic
		//		.lookup("java:global/IMusicStore/CartBean");

		try {
			// In case of delete, Delete item from shopping cart
			if (objForm.getAction().equals("delete")) {
				int productid = Integer.parseInt(request.getParameter("pid"));
				// Delete Item from cart
				CartBean.removeMusicCd(productid);

				// Set to the request attribute to show the items in the cart
				request.setAttribute("cdItems", CartBean.getContents());

				objForm.setAction("Edit");
			}

			// In case of Edit retrieve the data from datbase and set the values
			// in the form obj
			if (objForm.getAction().equals("Edit")) {
				if (request.getParameter("totalitems") != null) {
					int totalitms = Integer.parseInt(request
							.getParameter("totalitems"));

					for (int i = 1; i < totalitms; i++) {

						CartItem item = new CartItem();

						// Set values
						item.setitemId(Integer.parseInt(request
								.getParameter("itemId" + i)));

						// Set Item quantity
						item.setQty(Integer.parseInt(request
								.getParameter("quantity" + i)));
						// Update to cart
						CartBean.addMusicCd(item);

					}
				} else {
					MusicStoreBLBean = (MusicStoreBLRemote) ic
							.lookup("java:global/IMusicStore/MusicStoreStalessBean");

					CartItem item = new CartItem();
					if (objForm.getProductid() != null) {
						item.setitemId(MusicStoreBLBean.getMusicCdById(
								objForm.getProductid()).getIdCd());
						item.setItemName(MusicStoreBLBean.getMusicCdById(
								objForm.getProductid()).getTitle());
						item.setQty((1));
					}
					else
					{		
						item.setitemId(MusicStoreBLBean.getMusicCdById(Integer.parseInt(request
								.getParameter("cdId"))).getIdCd());
						item.setItemName(MusicStoreBLBean.getMusicCdById(Integer.parseInt(request
								.getParameter("cdId"))).getTitle());
						item.setQty((1));
					}

					CartBean.addMusicCd(item);
				}

				// Set to the request attribute to show the items in the cart
				request.setAttribute("cdItems", CartBean.getContents());

				// for the edit form
				forward2Page = "input";
				// Set the action to update
				objForm.setAction("Edit");
			}

		} catch (Exception e) {
			forward2Page = "input";
			System.out.println("Error:" + e.getMessage());

		}
		// Display the registration form to the user
		return mapping.findForward(forward2Page);
	}
}
